package com.wellink.viewswitchdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class ImageSwitchActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory{

    ImageSwitcher switcher;
    int[] imgIds = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imgswitch_layout);

        switcher = findViewById(R.id.switcher);
        switcher.setFactory(this);
        switcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
        switcher.setImageResource(imgIds[0]);

        switcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index ++;
                if(index >= imgIds.length){
                    index = 0;
                }

                switcher.setImageResource(imgIds[index]);
            }
        });

    }

    @Override
    public View makeView() {
        ImageView view = new ImageView(this);
        view.setBackgroundColor(0xFF000000);

        view.setScaleType(ImageView.ScaleType.FIT_XY);
        view.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        return view;
    }
}
